﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace Ko
{

    public class KoWS_Helper
    {

        #region Common

        public Exception GetErrList(ref byte[] parData, ref List<string> parErrList)
        {
            Exception ret = null;
            try
            {
                StringBuilder sb = new StringBuilder();
                foreach (byte bt in parData)
                    sb.Append((char)bt);
                string str = sb.ToString();
                XDocument xErrors = XDocument.Parse(str);
                var query = from g in xErrors.Descendants("Errors")
                            select new
                            {
                                errName = (string)g.Element("Err")
                            };
                foreach (var node in query)
                {
                    if (node.errName == null)
                    {
                        // Errors not found
                    }
                    else
                    {
                        parErrList.Add(node.errName);
                    }
                }
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }

        #endregion

        #region Base Methods

        public Exception Get(ref DataTable tbl, ref List<string> errList, string UrlStr)
        {
            Exception ret = null;
            try
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                wc.Headers["Cache-Control"] = "no-cache";
                wc.UseDefaultCredentials = false;
                wc.Credentials = new System.Net.NetworkCredential("anonymous", "janeDoe@contoso.com");
                wc.UseDefaultCredentials = false;
                byte[] data = wc.DownloadData(UrlStr + @"?guid=" + new Guid().ToString());
                if (tbl == null)
                    tbl = new DataTable("tbl");
                if (data.Length > 0)
                {
                    using (System.IO.MemoryStream xmlStream = new System.IO.MemoryStream())
                    {
                        xmlStream.Write(data, 0, data.Length);
                        xmlStream.Position = 0;
                        tbl.ReadXml(xmlStream);
                        xmlStream.Position = 0;
                        if(errList == null)
                            errList = new List<string>();
                        GetErrList(ref data, ref errList);
                        if (errList.Count > 0)
                            throw new Exception(errList[0]);
                    }
                }
                else
                    throw new Exception(@"Web Service http://sp.korablik.local:88 не вернул данных");
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }

        #endregion

        #region Get Catalogs

        public Exception GetSPUsersList(ref DataTable tbl, ref List<string> errList)
        {
            return Get(ref tbl, ref errList, @"http://sp.korablik.local:88/ws/Catalog/Users.aspx");
        }

        #endregion

    }

}
